/**
 * constant menu
 */
const Controller = require('egg').Controller;

class MenuController extends Controller {
  // menu list
  async menu_list() {
    try {
      const data = await this.ctx.service.menu.menu_list();
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: data
      }
    }catch(err) {
      return this.ctx.body = {
				s: 0,
				m: `Fail~${err}`
			}
    }
  }
  // menu add
  async menu_add() {
    const query = this.ctx.request.body;
      // {path, name, component, index, title, icon, show, type, permi, pid} = query;
    // this.validInput(query, ['type']);
    try {
      const dir_bol = this.validInput(query, ['path', 'component', 'title', 'type']);
      if(dir_bol !== 'true') return this.ctx.body = dir_bol;
      // 参数补全
      const comName = query.component.split('/');
      query['name'] = comName[comName.length - 1];
      const data = await this.ctx.service.menu.menu_add(query);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: data
      }
    }catch(err) {
      return this.ctx.body = {
				s: 0,
				m: `Fail~${err}`
			}
    }
  }
  // menu update
  async menu_update() {
    const query = this.ctx.request.body;
    try {
      const dir_bol = this.validInput(query, ['mid', 'path', 'component', 'title', 'type']);
      if(dir_bol !== 'true') return this.ctx.body = dir_bol;
      // 参数补全
      const comName = query.component.split('/'),
            mid = query.mid;
      query['name'] = comName[comName.length - 1];
      delete query['mid'];
      console.log('60...', mid, query);
      const data = await this.ctx.service.menu.menu_update(mid, query);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: data
      }
    }catch(err) {
      return this.ctx.body = {
				s: 0,
				m: `Fail~${err}`
			}
    }
  }
  // menu remove
  async menu_del() {
    const query = this.ctx.request.body;
    try {
      const dir_bol = this.validInput(query, ['mid']);
      if(dir_bol !== 'true') return this.ctx.body = dir_bol;

      const mid = query.mid;
      await this.ctx.service.menu.menu_del(mid);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: `ok~`,
        d: {}
      }
    }catch(err) {
      return this.ctx.body = {
				s: 0,
				m: `Fail~${err}`
			}
    }
  }
  // 必填项验证
  validInput(p, param) {
    for(const k of param) {
      if(p[k] === '' || p[k] === null || p[k] === undefined) return { s: 0, m: `fail~${k}为必要入参！`};
      if(isNaN(p[k])) {
        if(typeof p[k] === 'object' && Array.isArray(p[k]) && p[k].length < 1) return { s: 0, m: `fail~${k}为必要入参！`};
      }
    }
    return 'true';
  }
  // 空值验证  没有做Set&&Map类型判断
  hasVal(val) {
    if(val === '' || val === null || val === undefined) return false;
    if(isNaN(val)) {  // 非数字
      if(typeof val === 'object') {
        if(Array.isArray(val) && val.length < 1) {
          return false;
        }
      }
    }
    return true;
  }
}

module.exports = MenuController;